// Components/formatBox/index.js
import {
  Get,
  Post,
  JsonPost
} from "../../utils/request.js"
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    ifKan: {
      type: Boolean,
      value: false
    },
    open: {
      type: Boolean,
      value: false
    },
    bottom: {
      type: Boolean,
      value: false
    },
    height: {
      type: Boolean,
      value: false
    },
    goods_id: {
      type: String,
      value: '',
      observer: function(newVal, oldVal) {
        this.setData({
          goods_id:newVal
        })
      }
    },
    baseInfo: {
      type: Object,
      value: {},
      observer: function(newVal, oldVal) {
        this.setData({
          goodInfo:newVal
        })
      }
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    day_number:1,
    guiInfo:[],
    fuWuInfo:[],
    goodInfo:{},
    goods_id:'',
    // 选中规格index
    guiGindex:null,
    spec_id:null,
    color:null,
    size:null,
    price:null,
    transIndex:null,
    delivery_service_id:null,
    delivery_service:{},
    fuWuIndex:null,
    paving_service_id:null,
    FuQieIndex:null,
    machining_service_id:null,
    machining_num:null,
    paving_service:{},
    machining_service:{},
    cat_id:null,
    // 拼单价格
    discount_price:null
  },

  /**
   * 组件的方法列表t
   */
  methods: {
  // 选中规格
  clickGuig(e){
    // e.currentTarget.dataset.row
    let str='goodInfo.shop_price'
    let str1='goodInfo.img'
    this.setData({
      guiGindex:e.currentTarget.dataset.index,
      spec_id:e.currentTarget.dataset.row.id,
      color:e.currentTarget.dataset.row.color,
      size:e.currentTarget.dataset.row.size,
      price:e.currentTarget.dataset.row.price,
      discount_price:e.currentTarget.dataset.row.discount_price,
      [str]:e.currentTarget.dataset.row.price,
      [str1]:e.currentTarget.dataset.row.spec_img,
    })
  },
  // 选中送货服务
  clickTrans(e){
    // e.currentTarget.dataset.row
    if (e.currentTarget.dataset.index==this.data.transIndex) {
      this.setData({
        transIndex:null,
        delivery_service_id:null,
        delivery_service:{},
      })
      return
    }
    this.setData({
      transIndex:e.currentTarget.dataset.index,
      delivery_service_id:e.currentTarget.dataset.row.id,
      delivery_service:e.currentTarget.dataset.row,
    })
  },
  // 选中升级服务
  clickFuwu(e){
    // e.currentTarget.dataset.row
    let str='paving_service'
    if (e.currentTarget.dataset.index==this.data.fuWuIndex) {
      this.setData({
        fuWuIndex:null,
        paving_service_id:null,
        [str]:{},
      })
      return
    }
    this.setData({
      fuWuIndex:e.currentTarget.dataset.index,
      paving_service_id:e.currentTarget.dataset.row.id,
      [str]:e.currentTarget.dataset.row,
    })
  },
  // 选中切割服务
  clickFuQie(e){
    // e.currentTarget.dataset.row
    let str='machining_service'
    if (e.currentTarget.dataset.index==this.data.FuQieIndex) {
      this.setData({
        FuQieIndex:null,
        machining_service_id:null,
        [str]:{},
      })
      return
    }
    this.setData({
      FuQieIndex:e.currentTarget.dataset.index,
      machining_service_id:e.currentTarget.dataset.row.id,
      [str]:e.currentTarget.dataset.row,
    })
  },

  setlength(e){
    this.setData({
      machining_num:e.detail.value
    })
  },

  // 获取规格信息
  getInfo(e){
    wx.showLoading({
      title: '加载中',
    })
    if (this.data.goodInfo.spec_id) {
      this.setData({
        spec_id:this.data.goodInfo.spec_id
      })
    }
    let goods_id=this.properties.goods_id
    if (e) {
      goods_id=e
    }
    JsonPost('goods/spec',{goods_id}).then(res=>{
      this.setData({
        guiInfo:res.data
      })
    })
    JsonPost('service/list',{goods_id}).then(res=>{
      this.setData({
        fuWuInfo:res.data
      })
      wx.hideLoading()
    })
  },
  // 关闭
  wpxClose(){
    this.setData({
      open:false
    })
  },
  // 提交
  wpxSubmit(){ 
    if (this.data.spec_id==null||this.data.delivery_service_id==null) {
      wx.showToast({
        title: '请选择规格和送货服务',
        icon: 'none',
        duration: 2000
      })
      return
    } 
    if (this.data.machining_service_id&&!this.data.machining_num) {
      wx.showToast({
        title: '请输入需切割的长度',
        icon: 'none',
        duration: 2000
      })
      return
    } 

    let obj={
      cat_id:this.data.cat_id,
      num:this.data.day_number,
      goods_id:this.data.goods_id,
      spec_id:this.data.spec_id,
      delivery_service_id:this.data.delivery_service_id,
      paving_service_id:this.data.paving_service_id,
      delivery_service:this.data.delivery_service,
      paving_service:this.data.paving_service,
      machining_service:this.data.machining_service,
      color:this.data.color,
      size:this.data.size,
      price:this.data.price,
      discount_price:this.data.discount_price,
      machining_service_id:this.data.machining_service_id,
      machining_num:this.data.machining_num,
    }
    this.triggerEvent("getGuiGObj",obj)
    this.setData({
      open:false
    })

  },
  //减
  reduce: function(e) {
    var day_number = this.data.day_number;
    if (1 < day_number) {
      day_number--;
    } else {
      day_number = 1;
    }
    this.setData({
      day_number: day_number
    })
    // this.wpxjisuan()
  },
  //加
  add: function(e) {
    var day_number = this.data.day_number;
    day_number++;
    this.setData({
      day_number: day_number
    })
    // this.wpxjisuan()
  },

  setNum(e){
    let num=Number(e.detail.value)
    if (num<1) {
      this.setData({
        day_number:1
      })
    }else{
      this.setData({
        day_number:num
      })
    }
  },

  // 送货服务介绍
  explain(){
    wx.showModal({
      title: '服务介绍',
      content: '单笔订单满五万元,免费送货上门!! 未满五万元,商品将送至离您最近的物流站点',
      success (res) {
      }
    })
  },

  }
})
